package com.lzq.treefold.controller;

import com.lzq.treefold.dto.Result;
import com.lzq.treefold.entity.File;
import com.lzq.treefold.service.IFileService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lzq
 * @since 2025-06-06
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {

    @Resource
    IFileService fileService;

    @GetMapping("/{foldId}")
    public Result list(@PathVariable("foldId") Long foldId) {
        List<File> list = fileService.listSonFiles(foldId);
        return Result.ok(list);
    }

    @DeleteMapping
    public Result delete(@RequestParam Long id) {
        boolean result = fileService.delete(id);
        return Result.ok(result);
    }

    @PostMapping
    public Result add(@RequestBody File file) {
        return fileService.add(file) ? Result.ok() : Result.fail("存在重名文件");
    }

    @PutMapping
    public Result update(@RequestBody File file) {
        fileService.updateById(file);
        return Result.ok();
    }
}
